package com.cssw.fyzb.web.service;

import com.cssw.fyzb.base.web.domain.AjaxData;
import com.cssw.fyzb.base.web.domain.AjaxPage;
import com.cssw.fyzb.core.dao.live.GiftDao;
import com.cssw.fyzb.core.model.dodb.CmfGift;
import com.cssw.fyzb.core.model.dodb.SysFunc;
import com.cssw.fyzb.web.util.FileUtils;
import com.cssw.fyzb.web.util.FuncUtil;
import com.cssw.fyzb.web.vo.CmfGiftVo;
import com.cssw.fyzb.web.vo.ImgVo;
import com.ydn.appserver.MinaAppClient;
import com.ydn.appserver.Request;
import com.ydn.dbframe.plugin.activerecord.Db;
import com.ydn.dbframe.plugin.activerecord.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.math.BigDecimal;
import java.util.Map;

/**
 * @Author: tjf
 * @Date: 2020/10/10 15:09
 */
@Service
public class CmfGiftService {

    @Autowired
    private GiftDao giftDao;
    @Autowired
    private MinaAppClient tpsClient;


    /**
     * 查询礼物列表
     *
     * @param page
     * @param limit
     * @param param
     * @return
     */
    public AjaxPage list(int page, int limit, Map param) {
        Page<CmfGift> pages = giftDao.listGift(page, limit, param);
        return AjaxPage.success(pages.getList(), pages.getTotalRow(),pages.getTotalPage());
    }

    public AjaxData info(Long giftId) {
        return AjaxData.success(giftDao.queryById(giftId));
    }

    /**
     * 新增礼物
     */
    public AjaxData save(String type, Long cateId, String giftName, BigDecimal needCoin, BigDecimal giftYCoin, MultipartFile iconMiniFile, MultipartFile imageFile, int sort, Long sysOperId) throws Exception {
        Request request = new Request("AddRes");
        request.setParameter("type", "image");
        File file = FileUtils.multipartFileToFile(iconMiniFile);
        request.setParameter("extension", iconMiniFile.getOriginalFilename().substring(iconMiniFile.getOriginalFilename().lastIndexOf(".") + 1));
        request.setParameter("content", FileUtils.getBase64(file));
        String iconMini = tpsClient.send(request).getString("url");

        Request request2 = new Request("AddRes");
        request2.setParameter("type", "image");
        File file2 = FileUtils.multipartFileToFile(imageFile);
        request2.setParameter("extension", imageFile.getOriginalFilename().substring(imageFile.getOriginalFilename().lastIndexOf(".") + 1));
        request2.setParameter("content", FileUtils.getBase64(file2));
        String image = tpsClient.send(request2).getString("url");

        giftDao.addCmfGift(type, cateId, giftName, needCoin, giftYCoin, iconMini, image, sort, sysOperId);
        return AjaxData.success();
    }



    /**
     * 修改礼物
     */
    public AjaxData update(Long giftId, String type, Long cateId, String giftName, BigDecimal needCoin, BigDecimal giftYCoin, MultipartFile iconMiniFile, MultipartFile imageFile, int sort, Long sysOperId) throws Exception {
        String iconMini = null;
        String image = null;
        if(iconMiniFile != null) {
            Request request = new Request("AddRes");
            request.setParameter("type", "image");
            File file = FileUtils.multipartFileToFile(iconMiniFile);
            request.setParameter("extension", iconMiniFile.getOriginalFilename().substring(iconMiniFile.getOriginalFilename().lastIndexOf(".") + 1));
            request.setParameter("content", FileUtils.getBase64(file));
            iconMini = tpsClient.send(request).getString("url");
        }
        if(imageFile != null) {
            Request request2 = new Request("AddRes");
            request2.setParameter("type", "image");
            File file2 = FileUtils.multipartFileToFile(imageFile);
            request2.setParameter("extension", imageFile.getOriginalFilename().substring(imageFile.getOriginalFilename().lastIndexOf(".") + 1));
            request2.setParameter("content", FileUtils.getBase64(file2));
            image = tpsClient.send(request2).getString("url");
        }
        giftDao.updateCmfGift(giftId,type, cateId, giftName, needCoin, giftYCoin, iconMini, image, sort, sysOperId);
        return AjaxData.success();
    }


    /**
     * 删除礼物
     *
     * @param roleId
     */
    public AjaxData delete(Long roleId,Long sysOperId) {
        return AjaxData.success(giftDao.daleteCmfGift(roleId,sysOperId));
    }

    public AjaxPage listCmfLiveGift(int pageNumber, int pageSize){
        Page pages = giftDao.listCmfLiveGift(pageNumber,pageSize);
        return AjaxPage.success(pages.getList(), pages.getTotalPage(), pages.getTotalRow());
    }
}
